<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link rel="stylesheet" type="text/css" href="../../css/mui.css"/>
		<link href="../../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="../../css/general.css"/>
		<link rel="stylesheet" type="text/css" href="../../css/mui.picker.min.css"/>
		<link rel="stylesheet" type="text/css" href="../../css/mui.poppicker.css"/>
		<style type="text/css">
			.dtop{
				width: 100%;
				padding: 0.8em;
				display: flex;
				justify-content: flex-start;
				align-items: center;
			}
			
			.dtop>button{
				margin: 0 0.5em;
			}
			.content{
				width: 95%;
				margin: auto;
			}
			.dmain{
				display: flex;
				align-items: center;
				justify-content: flex-start;
				padding: 0.3em;
				background-color: white;
			}
			.dmain>div{
				width: 20%;
			}
			input{
				border: 0;
				height: 35px;
				border-bottom: 1px solid #C7C7C7;
			}
			.dbut{
				width: 60%;
				margin: 2em auto;
			}
			
table{
	width: 100%;
	text-align: center;
}
tr{
	padding: 0.3em;
}
td{
	padding: 0.3em;
}
tr>td:first-child{
	width: 30%;
}
tr>td:nth-child(2){
	width: 30%;
}
tr>td:nth-child(3){
	width: 10%;
}
tr>td:nth-child(4){
	width: 30%;
}

input[type=number]{
    width: 100%;
    margin-bottom: 0;
    padding-left: 0;
    border: 0;
    border-bottom: 1px solid #c7c7c7;
    line-height: 21px;
    height: 40px;
}

.butdiv{
	min-width: 50%;
	padding: 0.2em;
	margin: auto;
	border: 1px solid #c7c7c7;
	border-radius:5px;
}

.lcs-select{
	background-color: #007aff;
	color: white !important;
}
.lcs-noselect{
	background-color: white;
	color: #007aff;
}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav hbg">
		    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left colorw"></a>
		    <h1 class="mui-title">提成设置</h1>
		</header>
		<div class="mui-content">
			 <div id="">
		    <div class="dtop">
		    	提成基数： 
		    	<button type="button" id="tcyj" data-id="1" class="mui-btn lcs-select mui-btn-outlined lcs-butbase">原价</button>
		    	<!--
                	作者：592176224@qq.com
                	时间：2019-04-08
                	描述：
                	<button type="button" id="tcss" data-id="2" class="mui-btn lcs-noselect mui-btn-outlined lcs-butbase">实收</button>
                -->
		    </div>
		    	<div class="maindiv">
				<table border="0" cellspacing="20" cellpadding="20">
					<tr>
						<td>指定</td>
						<td><input id="appoint_percent" type="number" name="" value="" /></td>
						<td class="td tctd">%</td>
						<td><div id="proportion" class="butdiv">
							比例
						</div></td>
					</tr>
					<tr>
						<td>非指定</td>
						<td><input id="non_appoint_percent" type="number" name="" value="" /></td>
						<td class="td tctd">%</td>
						<td></td>
					</tr>
				</table>
		    </div>
		    <div class="dbut">
		    	<button id="subid" type="button" class="mui-btn mui-btn-blue mui-btn-block">保存</button>
		    </div>
		    </div>
		    <div id="ckdiv" class="hidden">
		    <div class="dtop">
		    	次卡扣减提成基数： 
		    	<button type="button" id="ckyj" data-id="1" class="mui-btn lcs-select mui-btn-outlined lcs-butbase ck-but">原价</button>
		    	<!--
                	作者：592176224@qq.com
                	时间：2019-04-08
                	描述：
                	<button type="button" id="ckss" data-id="2" class="mui-btn lcs-noselect mui-btn-outlined lcs-butbase ck-but">实收</button>
                -->
		    </div>
		    	<div class="maindiv">
				<table border="0" cellspacing="20" cellpadding="20">
					<tr>
						<td>指定</td>
						<td><input id="ckappoint_percent" type="number" name="" value="" /></td>
						<td class="td cktd">%</td>
						<td><div id="ckproportion" class="butdiv">
							比例
						</div></td>
					</tr>
					<tr>
						<td>非指定</td>
						<td><input id="cknon_appoint_percent" type="number" name="" value="" /></td>
						<td class="td cktd">%</td>
						<td></td>
					</tr>
					
					
				</table>
		    </div>
		    <div class="dbut">
		    	<button id="cksubid" type="button" class="mui-btn mui-btn-blue mui-btn-block">保存</button>
		    </div>
		    </div>
		    
		</div>
		<script src="../../js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../js/mui.min.js"></script>
		<script src="../../js/mui.picker.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../js/mui.poppicker.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../js/city.data.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../js/city.data-3.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../js/req.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../js/muishow.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			var rid = 0,		//角色id
			basetype = 1,		//1原价  2实收 		服务提成
			basetype2 = 1,		//1原价  2实收		次卡提成
			proportiontype = 1,	//1比例 2金额		服务
			proportiontype2 = 1,	//1比例 2金额	次卡
			product_id = 0,		//商品id 为0 是全部
			product_type = 0,	//提成类型 1商品 2服务 3次卡
			is_card = 2;	//是否是次卡 1是  2不是
			var appoint = {
				appoint_percent:0,
				non_appoint_percent:0,
				appoint_money : 0,
				non_appoint_money:0,
				ckappoint_percent : 0,
				cknon_appoint_percent : 0,
				ckappoint_money : 0,
				cknon_appoint_money : 0
			}
			
			window.onload = function(){
				mui.init();
				rid = getrequest().rid||0;
				product_id =parseInt(getrequest().product_id)||0;
				product_type = parseInt(getrequest().product_type)||0;
				//gid = getrequest().gid||0;
				console.log("product_type:",product_type);
				console.log(product_id);
				mui.plusReady(function(res){
					var view = plus.webview.currentWebview();
					product_id = view.product_id||0;
					product_type = view.product_type||0;
					rid = view.rid||0;
				})
				
				
				
				//监听 提成基数 
				$(".dtop .lcs-butbase").on("tap",function(e){
					basetype = $(this).attr("data-id");
					$(this).removeClass("lcs-noselect").addClass("lcs-select").siblings().removeClass("lcs-select").addClass("lcs-noselect")
				})
				
				//监听 次卡提成基数 
				$(".dtop .ck-but").on("tap",function(e){
					basetype2 = $(this).attr("data-id");
					$(this).removeClass("lcs-noselect").addClass("lcs-select").siblings().removeClass("lcs-select").addClass("lcs-noselect")
				})
				
				//设置类型
				var proportionPicker = new mui.PopPicker();
                proportionPicker.setData([{
                    value: '1',
                    text: '比例'
                },{
                    value: '2',
                    text: '金额'
                }]);
                //监听提成设置类型
                var proportion = document.getElementById('proportion');
                proportion.addEventListener('tap', function(event) {
                    proportionPicker.show(function(items) {
                        proportion.innerText = items[0].text;
                        proportiontype = items[0].value;
                        if(items[0].value == '1'){
                        	$(".tctd").html("%");
                        	g("appoint_percent").value = appoint.appoint_percent;
							g("non_appoint_percent").value = appoint.non_appoint_percent;
                        }
                        if(items[0].value == '2'){
                        	$(".tctd").html("元");
                        	g("appoint_percent").value = appoint.appoint_money;
							g("non_appoint_percent").value = appoint.non_appoint_money;
                        }
                        
                    });
                }, false);
                
                //监听次卡类型变化
                var ckproportion = document.getElementById('ckproportion');
                ckproportion.addEventListener('tap', function(event) {
                    proportionPicker.show(function(items) {
                        ckproportion.innerText = items[0].text;
                        proportiontype2 = items[0].value;
                        if(items[0].value == '1'){
                        	$(".cktd").html("%");
                        	g("ckappoint_percent").value = appoint.ckappoint_percent;
							g("cknon_appoint_percent").value = appoint.cknon_appoint_percent;
                        }
                        if(items[0].value == '2'){
                        	$(".cktd").html("元");
                        	g("ckappoint_percent").value = appoint.ckappoint_money;
							g("cknon_appoint_percent").value = appoint.cknon_appoint_money;
                        }
                    });
                }, false);
				
				//监听点击保存
				g("subid").addEventListener("tap",function(e){
					var data = {
						url:"/api/product_percent/set",
						data:{
							access_token:acctoken(),
							role_id:rid>0?rid:"",
							product_id:product_id,
							percent_base:basetype,
							product_type:product_type,
							type:proportiontype,
							appoint_percent: proportiontype==1?g("appoint_percent").value:"",
							non_appoint_percent:proportiontype==1?g("non_appoint_percent").value:"",
							appoint_money:proportiontype ==2?g("appoint_percent").value:"",
							non_appoint_money:proportiontype==2?g("non_appoint_percent").value:"",
							is_card:2,
						}
					}
					ajax(data,function(res){
						console.log("提成设置",res)
						mui.toast(res.msg);
						get_percent();
					})
					
				})
				
				//监听次卡保存
				g("cksubid").addEventListener("tap",function(e){
					var data = {
						url:"/api/product_percent/set",
						data:{
							access_token:acctoken(),
							role_id:rid>0?rid:"",
							product_id:product_id,
							percent_base:basetype2,
							product_type:3,
							type:proportiontype2,
							appoint_percent: proportiontype2==1?g("ckappoint_percent").value:"",
							non_appoint_percent:proportiontype2==1?g("cknon_appoint_percent").value:"",
							appoint_money:proportiontype2 ==2?g("ckappoint_percent").value:"",
							non_appoint_money:proportiontype2==2?g("cknon_appoint_percent").value:"",
							is_card:1,
						}
					}
					ajax(data,function(res){
						console.log("提成设置",res)
						mui.toast(res.msg);
						get_percent_card();
					})
					
				})
				get_percent();
				//如果 是不是商品 显示次卡
				if(product_type != 1){
					$("#ckdiv").removeClass("hidden");
					get_percent_card();
					
				}
			}
			//获取服务提成
			function get_percent(){
				var data = {
					url:'/api/employee/get_percent',
					data:{
						access_token:acctoken(),
						role_id:rid,
						product_id:product_id,
						product_type:product_type,
						is_card:2,
					}
				}
				ajax(data,function(res){
					console.log("提成",res);
					var ap,am,nap,nam;
					if(product_id > 0 || res.data.custom){
							ap = res.data.custom.appoint_percent;
							nap = res.data.custom.non_appoint_percent;
							am = res.data.custom.appoint_money;
							nam = res.data.custom.non_appoint_money;
					}else if(product_id == 0 && res.data.default){
							ap = res.data.default.appoint_percent;
							nap = res.data.default.non_appoint_percent;
							am = res.data.default.appoint_money;
							nam = res.data.default.non_appoint_money;
					}
					if(res.data.type == 1){
						g("appoint_percent").value = ap;
						g("non_appoint_percent").value = nap;
					}else{
						proportiontype = 2;
						$(".tctd").html("元");
						g("proportion").innerHTML = "金额";
						g("appoint_percent").value = am;
						g("non_appoint_percent").value = nam;
					}
					appoint.appoint_money = am;
					appoint.non_appoint_money = nam;
					appoint.appoint_percent = ap;
					appoint.non_appoint_percent = nap;
					/*if(res.data.percent_base == 2){
						$("#tcss").removeClass("lcs-noselect").addClass("lcs-select").siblings().removeClass("lcs-select").addClass("lcs-noselect")
					}*/
				})
			}
			//获取次卡提成
			function get_percent_card(){
				var data = {
					url:'/api/employee/get_percent',
					data:{
						access_token:acctoken(),
						role_id:rid,
						product_id:product_id,
						product_type:3,
						is_card:1,
					}
				}
				ajax(data,function(res){
					console.log("次卡提成",res);
					var ap,am,nap,nam;
					if(product_id > 0 && res.data.custom){
						ap = res.data.custom.appoint_percent;
						nap = res.data.custom.non_appoint_percent;
						am = res.data.custom.appoint_money;
						nam = res.data.custom.non_appoint_money;
					}else if(product_id == 0 && res.data.default){
						ap = res.data.default.appoint_percent;
						nap = res.data.default.non_appoint_percent;
						am = res.data.default.appoint_money;
						nam = res.data.default.non_appoint_money;
					}
					if(res.data.type == 1){
						g("ckappoint_percent").value = ap;
						g("cknon_appoint_percent").value = nap;
					}else{
						proportiontype = 2;
						$(".cktd").html("元");
						g("ckproportion").innerHTML = "金额";
						g("ckappoint_percent").value = am;
						g("cknon_appoint_percent").value = nam;
					}
					appoint.ckappoint_money = am;
					appoint.cknon_appoint_money = nam;
					appoint.ckappoint_percent = ap;
					appoint.cknon_appoint_percent = nap;
					/*if(res.data.percent_base == 2){
						$("#ckss").removeClass("lcs-noselect").addClass("lcs-select").siblings().removeClass("lcs-select").addClass("lcs-noselect")
					}*/
				})
			}
		</script>
	</body>

</html>